/*
 * Functions begin with:
 * dynarrTYPE, i.e., dynarri means dynamic int array.
 */

#include <stdlib.h>
#include "dyn_arr.h"
#include "wraplinux.h"

void
darri_init(darri *arr, int init_elems)
{
	arr->arr = bpz_malloc(init_elems * sizeof(int));
	arr->alloced = init_elems;
	arr->pos = 0;
}

void
darri_put(darri *arr, int x)
{
	// pos is an index that starts from 0 and alloced is an actual number
	// telling how MANY elements is space allocated for.
	if ( arr->pos == arr->alloced )
	{
		// Double the number each allocation as doing it too many times
		// is expensive.
		arr->alloced *= 2;

		// Out of space. Allocate new.
		arr->arr = bpz_realloc(arr->arr, arr->alloced * sizeof(int));
	}
	arr->arr[arr->pos] = x;
	arr->pos++;
}
